<%@ page import="java.net.URLDecoder" %>
<%@ page import="java.text.DateFormat" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        response.setContentType("text/html;charset=utf-8");
        Cookie[] cookies = request.getCookies();
        boolean flag = false ;
        if(cookies != null && cookies.length > 0){
            for(Cookie cookie : cookies){
                String name = cookie.getName();
                if("lastTime".equals(name)){
                    flag = true;
                    String value = cookie.getValue();
                    value = URLDecoder.decode(value,"UTF-8");

                    DateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                    String format1 = format.format(new Date());
                    format1 = URLEncoder.encode(format1, "UTF-8");
                    cookie.setValue(format1);
                    cookie.setMaxAge(30 * 24 * 60 * 60);
                    response.addCookie(cookie);

                    response.getWriter().write("欢迎回来, 您上次的登陆时间为:" + value);
                    break;
                }
            }
        }

        if(!flag){
            DateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String format1 = format.format(new Date());
            format1 = URLEncoder.encode(format1, "UTF-8");
            Cookie cookie = new Cookie("lastTime", format1);
            cookie.setValue(format1);
            cookie.setMaxAge(30 * 24 * 60 * 60);
            response.addCookie(cookie);

            response.getWriter().write("您好, 欢迎首次访问");
        }

    %>
</body>
</html>
